<template>
	<view>
		<uni-forms ref="baseForm" :model="baseFormData" labelWidth="160rpx" class="form">
			<uni-forms-item label="标题">
				<uni-easyinput v-model="baseFormData.name" placeholder="XXX店 产品" />
			</uni-forms-item>
			<uni-forms-item label="支付方式">
				<uni-data-picker v-model="baseFormData.qudao" :localdata="qudao" popup-title="选择渠道">
				</uni-data-picker>
			</uni-forms-item>
			<uni-forms-item label="任务金额">
				<uni-easyinput v-model="baseFormData.jine" placeholder="请输入金额" />
			</uni-forms-item>
			<uni-forms-item label="任务单价">
				<uni-easyinput v-model="baseFormData.danjia" placeholder="请输入单价" />
			</uni-forms-item>
			<uni-forms-item label="任务个数">
				<uni-number-box v-model="baseFormData.number" class="num" />
			</uni-forms-item>
			<uni-forms-item label="收款码">
				<uni-file-picker fileMediatype="image" :limit="1" mode="grid" @select="uloadFile" />
			</uni-forms-item>
			<uni-forms-item label="保证金">
				<p class="bzj">999</p>
			</uni-forms-item>
			<button class="button" type="primary" @click="fabu">发布</button>
		</uni-forms>
	</view>
</template>

<script>
	import {
		baseDefaultUrl
	} from '../../config/config';
	export default {
		data() {
			return {
				styles: {
					color: '#2979FF',
					borderColor: '#2979FF'
				},
				baseFormData: {
					name: '',
					number: 1,
					qudao: '',
					jine: '',
					danjia: '',
					images:'',
					baozheng: 999
				},
				qudao: [{
						text: "微信零钱",
						value: "微信零钱",
					},
					{
						text: "微信借记卡",
						value: "微信借记卡",
					},
					{
						text: "微信信用卡",
						value: "微信信用卡",
					},
					{
						text: "支付宝零钱",
						value: "支付宝零钱",
					},
					{
						text: "支付宝借记卡",
						value: "支付宝借记卡",
					},
					{
						text: "支付宝零钱",
						value: "支付宝零钱",
					},
					{
						text: "云闪付",
						value: "云闪付",
					},
				]
			}
		},
		methods: {
			uloadFile(e) {
				const tempFilePaths = e.tempFilePaths;
				uni.uploadFile({
					url: baseDefaultUrl + '/inter/upload/fileUp',
					filePath: tempFilePaths[0],
					name: 'file',
					// formData: {
					// 	'user': 'test'
					// },
					success: (uploadFileRes) => {
						console.log(uploadFileRes.data);
						let ress = JSON.parse(uploadFileRes.data)
						this.baseFormData.images = ress.data
					}
				});
			},
			allExist(obj) {
				return Object.values(obj).every(value => {
					return value !== null && value !== undefined && value !== '';
				})
			},
			fabu() {
				if (this.allExist(this.baseFormData)) {
					uni.request({
						url: baseDefaultUrl + "/inter/t-work/save",
						method: 'POST',
						data: {
							'title': this.baseFormData.name,
							'qudao': this.baseFormData.qudao,
							'jine': this.baseFormData.jine,
							'geshu': this.baseFormData.number,
							'baozheng': this.baseFormData.baozheng,
							'username': uni.getStorageSync('phone'),
							'images': this.baseFormData.images,
							'yongjin': this.baseFormData.danjia
						},
						header: {
							"Authorization": uni.getStorageSync('token')
						},
						success: (res) => {
							if (res.data.code != 0) {
								uni.navigateBack({
									delta: 1,
									success: function() {
										setTimeout(function() {
											uni.showToast({
												title: "发布成功",
												icon: 'success',
												duration: 2000
											});
										}, 500);
									}
								});
							} else {
								uni.showToast({
									title: res.data.message,
									icon: 'error'
								});
							}
						}
					})
				} else {
					uni.showToast({
						title: "请完善所有内容",
						icon: 'error'
					});
				}
			},
			// uloadFile() {
			// 	uni.chooseImage({
			// 		success: (chooseImageRes) => {
			// 			const tempFilePaths = chooseImageRes.tempFilePaths;
			// 			uni.uploadFile({
			// 				url: baseDefaultUrl + '/inter/upload/fileUp', //仅为示例，非真实的接口地址
			// 				filePath: tempFilePaths[0],
			// 				name: 'file',
			// 				// formData: {
			// 				// 	'user': 'test'
			// 				// },
			// 				success: (uploadFileRes) => {
			// 					console.log(uploadFileRes.data);
			// 					let ress = JSON.parse(uploadFileRes.data)
			// 					this.baseFormData.images = ress.data
			// 				}
			// 			});
			// 		}
			// 	});
			// }
		}
	}
</script>

<style scoped>
	.form {
		padding: 20rpx;
	}

	.num {
		margin-top: 10rpx;
	}

	.bzj {
		margin-top: 10rpx;
		font-size: 40rpx;
		font-weight: bolder;
	}
</style>